import registnet.records.grade.CompletionGradeSheet;
import registnet.records.grade.source.CompletionGradeSheetSource;
import registnet.records.Student;
import registnet.records.Grade;

import registnet.dao.GradePeer;
import registnet.dao.CoursePeer;
import registnet.dao.StudentPeer;
import registnet.dao.SystemPropertiesPeer;
import registnet.dao.SystemProperties;

import resources.Constants;

GradePeer gPeer = new GradePeer();

/* [start] 	initializes gui variables */
String _completion = bundle.getProperty("registnet.module.grades.completion");

String _set_grade = bundle.getProperty("registnet.module.grades.completion.actions.set_grade");

String _import = bundle.getProperty("registnet.module.data_management.actions.menu.import");
String _student_number = bundle.getProperty("registnet.module.grades.macro.completion.form.student_number");
String _full_name = bundle.getProperty("registnet.module.grades.macro.completion.form.full_name");
String _academic_year = bundle.getProperty("registnet.module.grades.macro.completion.form.academic_year");
String _semester = bundle.getProperty("registnet.module.grades.macro.completion.form.semester");
String _course = bundle.getProperty("registnet.module.grades.macro.completion.form.course");
String _section = bundle.getProperty("registnet.module.grades.macro.completion.form.section");
String _grade = bundle.getProperty("registnet.module.grades.macro.completion.form.grade");
String _remarks = bundle.getProperty("registnet.module.grades.macro.completion.form.remarks");
String _submit = bundle.getProperty("registnet.module.grades.macro.completion.form.submit");
String _cancel = bundle.getProperty("registnet.module.grades.macro.completion.form.cancel");

String _registrar = Constants.REGISTRAR; 

String _icon_import = bundle.getProperty("registnet.icons.import");
/* [end] */

/* setting the user for logs */
if(_user_type.equals(_registrar))
	gPeer.setUser(_user, _registrar);
else
	gPeer.setUser(_user, _office);
/* [end] */

/* [start]	initializes the grades */
CompletionGradeSheet completionGradeSheet = new CompletionGradeSheetSource().getCompletionGradeSheet();
List grades = completionGradeSheet.getGrades();
/* [end] */



/* [start]	defines a renderer for the grades */
private class CompletionGradeSheetRenderer implements ListitemRenderer  {
	public void render(Listitem item, java.lang.Object data) {
		Student student = data.getStudent();
		new Listcell(student.getStudentNumber()).setParent(item);
		new Listcell(student.getLastName() + " " + student.getFirstName() + " " + student.getMiddleName()).setParent(item);
		if (data.getSemester() == Constants.MAP_SEMESTER_KEY.get(Constants.SUMMER))
			new Listcell((data.getYear() - 1) + "-" + data.getYear()).setParent(item);
		else
			new Listcell(data.getYear() + "-" + (data.getYear() + 1)).setParent(item);
		new Listcell(Constants.MAP_SEMESTER_VALUE.get(data.getSemester())).setParent(item);
		new Listcell(data.getCourse().getCourseCode()).setParent(item);
		new Listcell(data.getSection().getSectionName()).setParent(item);
		
		boolean isLapsed = false;
		SystemProperties systemProperties = new SystemPropertiesPeer().getSystemProperties();
		if ((systemProperties.getAcademicYear() - data.getYear()) > 1){
			if (systemProperties.getSemester() - data.getSemester() < 0)
				isLapsed = false;
			else
				isLapsed = true;
		}
		else
			isLapsed = false;
		
		if (isLapsed)
			new Listcell("LAPSED").setParent(item);
		else
			new Listcell("").setParent(item);
	}
}
/* [end] */



/* [start] 	retrieves the grades and places them on the listbox */
void retrieveGrades(){
	ListIterator iter = grades.listIterator();
	List listModel = new Vector();
	while(iter.hasNext()){
		Grade grade = iter.next();
		Student student = grade.getStudent();
		if (_user_type.equals(Constants.REGISTRAR) || (_user_type.equals(Constants.COLLEGE) && student.getCurriculum().getDegreeProgram().getCollege().getCollegeCode().equals(_office)))
			listModel.add(grade);
	}
	ListModel strset = new SimpleListModel(listModel);
	completiongradesheet_l.setModel(strset);
	completiongradesheet_l.setItemRenderer(new CompletionGradeSheetRenderer());
}
/* [end] */



/* [start]	creates a new grades entry in the database;
			removes the selected item from the listbox
			notifies the user if no grade is selected */
void submit(){
	if(contents_win.getFellow("completiongradesheet_l").getSelectedItems().size() != 0){
		try{
			String sectionNameValue = Constants.COMPLETION;
			Integer semesterValue = Constants.MAP_SEMESTER_KEY.get(semester.getSelectedItem().getLabel());
			String remarksValue = remarks.value;
			Integer creditValue = 0;
			Double gradeValue = Constants.MAP_GRADE_KEY.get(grade.getSelectedItem().getLabel());
			Integer academicYearValue;
			if (semesterValue == Constants.MAP_SEMESTER_KEY.get(Constants.SUMMER))
				academicYearValue = Integer.parseInt(year2.value);
			else
				academicYearValue = Integer.parseInt(year1.value);
			
			Iterator iter = contents_win.getFellow("completiongradesheet_l").getSelectedItems().iterator();
			int i=0;
			while(iter.hasNext()){
				Listitem item = iter.next();
				List listcells = item.getChildren();
				String courseCodeValue = listcells.get(4).getLabel();
				String studentNumberValue = listcells.get(0).getLabel();
				try{
					gPeer.createNewGrade(new CoursePeer().getCourseId(courseCodeValue), sectionNameValue, gradeValue, new StudentPeer().getStudentId(studentNumberValue), semesterValue, academicYearValue, remarksValue, creditValue);
					grades.remove(item.getIndex() - i);
					i++;
				}catch(Exception e){e.printStackTrace();}
			}
			ListModel strset = new SimpleListModel(grades);
			contents_win.getFellow("completiongradesheet_l").setModel(strset);
		}catch(Exception e) { alert("Check information supplied"); }
	}
	else
		alert("No grade selected");
}
/* [end] */



/* [start]	clears the form;
			closes the form */
void cancel(){
	year1.value = null;
	year2.value = null;
	semester.value = null;
	grade.value = null;
	remarks.value = null;
	set_grade_popup.close();
}
/* [end] */

		
		
/* [start]	automatically updates the 'to' value of academic year based on the
			current 'from' value of the academic year */
private void updateAY(){
	try{
		year2.value = (Integer.parseInt(event.value) + 1).toString();
	}catch(Exception e) {year1.value = null; year2.value = null;}
}
/* [end] */

void importData(){
	org.zkoss.util.media.Media media = Fileupload.get();
	if(media != null){
		java.io.Reader r = new java.io.BufferedReader(media.getReaderData());
		String line = null;
		StringBuffer buf = new StringBuffer(9999999);
			while ((line = r.readLine()) != null) {
			buf.append(line+"\r\n"); 
			}
		
		int linenum = gPeer.importCompletion(buf.toString());
		if(linenum==0){
			alert("successful");
		}
		else
			alert("failed "+linenum);
	}
}